package com.servicelayer.user;

import com.core.StringUtils;
import com.core.protocols.IHibernateParentEnumeratorDelegate;
import com.core.protocols.IKVObject;
import com.core.protocols.IServiceLayerDelegate;
import com.managers.hibernate.HibernateFetchRequest;
import com.model.KeyValue;
import com.model.user.User;
import com.servicelayer.base.HibernateServiceLayer;

public class UserListLayer
        extends HibernateServiceLayer {

    public Class <? extends IKVObject> entityClass () {

        return User.class;
    }



    public void enumerateParentObjectsWithDelegate ( IKVObject kvObject,
                                                     IHibernateParentEnumeratorDelegate delegate ) {

    }



    public void resetPassword ( Integer userId,
                                String value,
                                IServiceLayerDelegate delegate ) {

        KeyValue kv = new KeyValue("applyPasswordMD5String", value);

        this.setCustomProperty(userId, kv, delegate);

    }



    public void checkValidPassword ( String userName,
                                     String userPass ) {

        if ( userName == null ) {
            return;
        }
        String userMD5Pass = null;
        if ( userPass != null ) {
            userMD5Pass = String.format("'%s'", StringUtils.md5FromString(userPass));
        }


        HibernateFetchRequest request = new HibernateFetchRequest();
        request.setDelegate(this);
        String predicate = String.format("where USER.name='%s' and USER.passwordHashString=%s", userName, userMD5Pass);
        request.performFetch(User.class, predicate);

    }



}
